import routes from './path'
import { getToken } from '@/utils/userManage'
Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'hash',
  routes: [
    ...routes,
    { // 输入错误路径，则默认走到第一个路由
      path: '*',
      redirect: routes[ 0 ].path
    }
  ],
})

/* 路由守卫 */
router.beforeEach((to, _, next) => {
  /* 每次路由变化，需要把新路由的参数上报 */
  window.__POWERED_BY_QIANKUN__ && Vue.prototype.$rootApp.reportRoute(to.meta)

  if (window.__POWERED_BY_QIANKUN__ && to.meta.login && !getToken()) {
    Vue.prototype.$rootApp.noLogin()
  } else {
    next()
  }
})

Vue.prototype.$myRouter = router

export default router
